﻿/************************************************************************

   Extended Silverlight Toolkit

   Copyright (C) 2010-2012 Xceed Software Inc.

   This program is provided to you under the terms of the Microsoft Public
   License (Ms-PL) as published at http://extendedsilverlight.codeplex.com/license

   Please purchase a commercial version of this toolkit if you are using
   it in a commercial product. The commercial versions support the project,
   and also include more features for each control, priority support,
   source code, updates, removed watermark, and a proprietary license.

   Visit http://xceed.com and follow @datagrid on Twitter.

  **********************************************************************/

using System;
using System.Windows;

namespace Xceed.Silverlight.DataGrid
{
  internal class HeaderFooterWrapper : UIElementWrapper
  {



    public HeaderFooterWrapper( UIElement child, object key )
      : base( child )
    {
      m_key = key;
    }

    internal override object RecycleKey { get { return m_key; } }

    protected override double CalculatePositionY()
    {
      double y = base.CalculatePositionY();
      double transitionOffset = y;

      if (m_arrangeParameters.VisualArrangeLimits != null)
      {
        double arrangePosition = m_arrangeParameters.ArrangePosition.Y;
        double limit = m_arrangeParameters.VisualArrangeLimits.AbsolutePositionLimit;
        if (m_arrangeParameters.VisualArrangeLimits.IsTopLimit)
        {
          y += Math.Max(0d, limit - (arrangePosition + y));
        }
        else
        {
          arrangePosition += m_arrangeParameters.ArrangePosition.Height;
          y -= Math.Max(0d, (arrangePosition + y) - limit);
        }
      }

      if (m_arrangeParameters.OffsetLimits != null)
      {
        y -= transitionOffset;
        y = Math.Max(y, m_arrangeParameters.OffsetLimits.LowerLimit);
        y = Math.Min( y, m_arrangeParameters.OffsetLimits.UpperLimit );
        y += transitionOffset;
      }

      return y;
    }

    protected override void LayoutArrange( ArrangeParameters parameters )
    {
      m_arrangeParameters = parameters;
      this.UpdatePositionY();
      base.LayoutArrange( parameters );
    }

    public static object CreateKey( object template, int indexId, bool isHeader )
    {
      return new Tuple<object, int, bool>( template, indexId, isHeader );
    }

    private object m_key;

    private ArrangeParameters m_arrangeParameters;
  }
}
